<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>关闭 Oracle 连接</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.oci-close" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">oci_close</h1>
  <p class="verinfo">(PHP 5, PHP 7, PECL OCI8 &gt;= 1.1.0)</p><p class="refpurpose"><span class="refname">oci_close</span> &mdash; <span class="dc-title">关闭 Oracle 连接</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.oci-close-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>oci_close</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$connection</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>oci_close()</strong></span> 将 Oracle 连接
   <code class="parameter">connection</code> 关闭。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    自版本 1.1 起 <span class="function"><strong>oci_close()</strong></span> 正确关闭
    Oracle 连接。使用 <a href="oci8.configuration.html#ini.oci8.old-oci-close-semantics" class="link">oci8.old_oci_close_semantics</a>
    选项来恢复本函数的旧行为。
   </p>
  </p></blockquote>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
   </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    在 PHP 5.0.0 之前的版本必须使用 <span class="function"><a href="ocilogoff.html" class="function">ocilogoff()</a></span>
    替代本函数。该函数名仍然可用，为向下兼容作为
    <span class="function"><strong>oci_close()</strong></span> 的别名。不过其已被废弃，不推荐使用。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.oci-close-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">connection</code></dt>

     <dd>

      <p class="para">
       An Oracle connection identifier returned by 
       <span class="function"><a href="oci_connect.html" class="function">oci_connect()</a></span>, <span class="function"><a href="oci_pconnect.html" class="function">oci_pconnect()</a></span>,
       or <span class="function"><a href="oci_new_connect.html" class="function">oci_new_connect()</a></span>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.oci-close-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.oci-close-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-2394">
   <p><strong>Example #1 Closing a connection</strong></p>
   <div class="example-contents"><p>
    Resources associated with a connection should be closed to ensure
    the underlying database connection is properly terminated and the
    database resources are released.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$e&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_error</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;*&nbsp;FROM&nbsp;departments'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$r&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_fetch_all</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Free&nbsp;the&nbsp;statement&nbsp;identifier&nbsp;when&nbsp;closing&nbsp;the&nbsp;connection<br /></span><span style="color: #0000BB">oci_free_statement</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

  </div>

  <div class="example" id="example-2395">
   <p><strong>Example #2 Database connections are not closed until all references are closed</strong></p>
   <div class="example-contents"><p>
    The internal refcount of a connection identifier must be zero
    before the underlying connection to the database is closed.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$e&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_error</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;*&nbsp;FROM&nbsp;departments'</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;this&nbsp;increases&nbsp;the&nbsp;refcount&nbsp;on&nbsp;$conn<br /></span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">oci_fetch_all</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">oci_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;$conn&nbsp;is&nbsp;no&nbsp;long&nbsp;usable&nbsp;in&nbsp;the&nbsp;script&nbsp;but&nbsp;the&nbsp;underlying&nbsp;database<br />//&nbsp;connection&nbsp;is&nbsp;still&nbsp;held&nbsp;open&nbsp;until&nbsp;$stid&nbsp;is&nbsp;freed.<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;NULL&nbsp;&nbsp;<br /><br />//&nbsp;While&nbsp;PHP&nbsp;sleeps,&nbsp;querying&nbsp;the&nbsp;Oracle&nbsp;V$SESSION&nbsp;view&nbsp;in&nbsp;a<br />//&nbsp;terminal&nbsp;window&nbsp;will&nbsp;show&nbsp;that&nbsp;the&nbsp;database&nbsp;user&nbsp;is&nbsp;still&nbsp;connected.<br /></span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;When&nbsp;$stid&nbsp;is&nbsp;freed,&nbsp;the&nbsp;database&nbsp;connection&nbsp;is&nbsp;physically&nbsp;closed<br /></span><span style="color: #0000BB">oci_free_statement</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">);&nbsp;&nbsp;<br /><br /></span><span style="color: #FF8000">//&nbsp;While&nbsp;PHP&nbsp;sleeps,&nbsp;querying&nbsp;the&nbsp;Oracle&nbsp;V$SESSION&nbsp;view&nbsp;in&nbsp;a<br />//&nbsp;terminal&nbsp;window&nbsp;will&nbsp;show&nbsp;that&nbsp;the&nbsp;database&nbsp;user&nbsp;has&nbsp;disconnected.<br /></span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

  </div>

  <div class="example" id="example-2396">
   <p><strong>Example #3 Closing a connection opened more than once</strong></p>
   <div class="example-contents"><p>
     When database credentials are reused, both connections must be closed
     before the underlying database connection is closed.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$conn1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Using&nbsp;the&nbsp;same&nbsp;credentials&nbsp;reuses&nbsp;the&nbsp;same&nbsp;underlying&nbsp;database&nbsp;connection<br />//&nbsp;Any&nbsp;uncommitted&nbsp;changes&nbsp;done&nbsp;on&nbsp;$conn1&nbsp;will&nbsp;be&nbsp;visible&nbsp;in&nbsp;$conn2<br /></span><span style="color: #0000BB">$conn2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'welcome'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;While&nbsp;PHP&nbsp;sleeps,&nbsp;querying&nbsp;the&nbsp;Oracle&nbsp;V$SESSION&nbsp;view&nbsp;in&nbsp;a<br />//&nbsp;terminal&nbsp;window&nbsp;will&nbsp;show&nbsp;that&nbsp;only&nbsp;one&nbsp;database&nbsp;user&nbsp;is&nbsp;connected.<br /></span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">oci_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn1</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;doesn't&nbsp;close&nbsp;the&nbsp;underlying&nbsp;database&nbsp;connection<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn1</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;NULL&nbsp;because&nbsp;the&nbsp;variable&nbsp;$conn1&nbsp;is&nbsp;no&nbsp;longer&nbsp;usable<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn2</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;displays&nbsp;that&nbsp;$conn2&nbsp;is&nbsp;still&nbsp;a&nbsp;valid&nbsp;connection&nbsp;resource<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

  </div>

  <div class="example" id="example-2397">
   <p><strong>Example #4 Connections are closed when variables go out of scope</strong></p>
   <div class="example-contents"><p>
    When all variables referencing a connection go out of scope and
    are freed by PHP, a rollback occurs (if necessary) and the
    underlying connection to the database is closed.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">myfunc</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'hr'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'hrpwd'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'localhost/XE'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$e&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_error</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">oci_parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'UPDATE&nbsp;mytab&nbsp;SET&nbsp;id&nbsp;=&nbsp;100'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">oci_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">OCI_NO_AUTO_COMMIT</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"Finished"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$r&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">myfunc</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">//&nbsp;At&nbsp;this&nbsp;point&nbsp;a&nbsp;rollback&nbsp;occurred&nbsp;and&nbsp;the&nbsp;underlying&nbsp;database&nbsp;connection&nbsp;was&nbsp;released.<br /><br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$r</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;displays&nbsp;the&nbsp;function&nbsp;return&nbsp;value&nbsp;"Finished"<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-function.oci-close-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Variables that have a dependency on the connection identifier,
    such as statement identifiers returned
    by <span class="function"><a href="oci_parse.html" class="function">oci_parse()</a></span>, must also be freed before the
    underlying database connection is closed.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Prior to version PHP 5.1.2 (PECL OCI8
    1.1) <span class="function"><strong>oci_close()</strong></span> was a no-op. In more recent
    versions it correctly closes the Oracle
    connection. Use <a href="oci8.configuration.html#ini.oci8.old-oci-close-semantics" class="link">oci8.old_oci_close_semantics</a>
    option to restore old behavior of this function.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The <span class="function"><strong>oci_close()</strong></span> function does not close the
    underlying database connections created
    with <span class="function"><a href="oci_pconnect.html" class="function">oci_pconnect()</a></span>.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    In PHP versions before 5.0.0 you must
    use <span class="function"><a href="ocilogoff.html" class="function">ocilogoff()</a></span> instead. 在当前版本中，旧的函数名还可以被使用，但已经被废弃并不建议使用。
   </p>
  </p></blockquote>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.oci-close-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="oci_connect.html" class="function" rel="rdfs-seeAlso">oci_connect()</a> - 建立一个到 Oracle 服务器的连接</span></li>
    <li class="member"><span class="function"><a href="oci_free_statement.html" class="function" rel="rdfs-seeAlso">oci_free_statement()</a> - 释放关联于语句或游标的所有资源</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>